# -*- coding: utf-8 -*-
"""
Created on Sun Nov 24 20:58:18 2013

@author: Casa
"""
import math
import numpy as np
import os
import sys

class testset:
    '''
    Four value representation
    0:10:2 1:01:1 X:00:0 C:11:3
    '''
    def __init__(self,filename):
        '''
        read testset
        data,size
        '''
        self.name=os.path.basename(filename)
        raw_data=[]
        with open(filename) as f:
            for line in f:
                if len(line)>2:
                    raw_data.append(bytearray(line[:-1]))
        d=np.array(raw_data,np.int)
        d[d==88]=0
        d[d==48]=2
        d[d==49]=1
        self.data=d
		
def code(data):
    '''FDR coding'''
    __m={}#dictionary
    size=0
    i=0
    while i<data.size:
        k=i
        while k<data.size and data[k]!=1:
            k+=1
        n=k-i
        if not __m.has_key(n):
            group=int(math.ceil(math.log(n+3)/math.log(2)-1))
            l=group*2
            __m[n]=l
        size+=__m[n]
        i=k+1
    return size

def FDR(data):
    '''
    turn the testset into one string and apply FDR coding
    '''
    return code(data.reshape(-1))
    
if __name__=='__main__': 
    #from time import clock
    #t0=clock()
    ts=testset(sys.argv[1])
    n=FDR(ts.data)
    print ('%.2f'%((1-n*1.0/ts.data.size)*100))
    #print clock()-t0
